package pl.koziolekweb.szkola.DXBajty;

// DX-Bajty/Bajty.java
 
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;

public class Bajty {

    public static void main(String[] args) {
        new Bajty();
    }

    Bajty() {
        File f =
            new File("/home/werner/essential-20061022.tar");
                     //    new File("c:/winnt/regedit.exe");

        int ilecntr = 0, ileasci = 0, ileextd = 0, bajt,ile;
        long start  = 0, time = 0;

        try {
            FileInputStream fis = new FileInputStream(f);
            start = System.currentTimeMillis();
            while ( (bajt = fis.read()) != -1 ) {
                if      (bajt <  32) ++ilecntr;
                else if (bajt < 128) ++ileasci;
                else                 ++ileextd;
            }
            time = System.currentTimeMillis() - start;
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        info(f,ilecntr,ileasci,ileextd,time);

        ilecntr = ileasci = ileextd = 0;

        try {
            BufferedInputStream bis =
                new BufferedInputStream(
                new FileInputStream(f));
            start = System.currentTimeMillis();
            while ( (bajt = bis.read()) != -1 ) {
                if      (bajt <  32) ilecntr++;
                else if (bajt < 128) ileasci++;
                else                 ileextd++;
            }
            time = System.currentTimeMillis() - start;
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        info(f,ilecntr,ileasci,ileextd,time);

        ilecntr = ileasci = ileextd = 0;

        try {
            BufferedInputStream bis =
                new BufferedInputStream(
                new FileInputStream(f));
            byte[] bytes = new byte[8196];
            start = System.currentTimeMillis();
            while ( (ile = bis.read(bytes)) > 0 ) {
                for (int i = 0; i < ile; ++i) {
                    bajt = 0xff & bytes[i];
                    if      (bajt <  32) ilecntr++;
                    else if (bajt < 128) ileasci++;
                    else                 ileextd++;
                }
            }
            time = System.currentTimeMillis() - start;
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        info(f,ilecntr,ileasci,ileextd,time);
    }

    void info(File f, int ilecntr, int  ileasci,
                      int ileextd, long time) {
        System.out.println(
            "\nPlik " + f.getAbsolutePath() + "\no dlugos" +
            "ci " + f.length() + " bajtow zawiera:\n  " +
            ilecntr + " znakow kontrolnych\n  " +
            ileasci + " drukowalnych znakow ascii\n  " +
            ileextd + " \"rozszerzonych\" znakow ascii." +
            "\nCzas: " + (double)time/1000 + " sekund\n");
    }
}
